package business.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import model.T_Novel;
import model.V_Novel;
import business.basc.BaseDAO;
import business.basc.BaseDaoImpl;
import business.dao.NovelDAO;

public class NovelDAOimpl implements NovelDAO {
	private BaseDAO bdao  = null;
	
	public NovelDAOimpl() {
		bdao = new BaseDaoImpl();
	}
	@Override
	public int addNovel(T_Novel novel) {
		String procName ="up_addNovel(?,?,?,?,?)";
		Object para[]={novel.getNovelname(),novel.getAbstrac(),novel.getNoveltype(),
				novel.getNovelphoto(),novel.getUserid()};
		
		return (Integer) bdao.executeProduce(procName,para);
	}

	@Override
	public boolean deleteNovel(int novelid) {
		String procName ="up_deleteNovel(?)";
		Object para[]={novelid};
		Object obj = bdao.executeProduce(procName, para);
		int row = Integer.parseInt(String.valueOf(obj));
		if (row>0) {
			return true;
		}
		else {
			return false;
		}
		
	}

	@Override
	public boolean updateNovel(T_Novel Novel) {
		String sql = "update T_Novel set novelname=?,novelphoto=?,"
				+ "abstrac=?,noveltype=?,novelstatus=? where userid=? "
				+ "and novelid=?";
		Object[] para = {Novel.getNovelname(),Novel.getNovelphoto(),
				Novel.getAbstrac(),Novel.getNoveltype(),Novel.getNovelstatus(),
				Novel.getUserid(),Novel.getNovelid()};
		int row = bdao.update(sql,para);
		if (row>0) {
			return true;
		}
		else {
			return false;
		}
		
	}

	@Override
	public int allreadyReviewConter(int novelid) {
		String sql = "select *  from T_Review where novelid =?";
		Object[] para = {novelid};
		int count = bdao.selectCount(sql,para);
		return count;
	}

	@Override
	public boolean increaseReviewCounter(int novelid) {
		int count = 0 ;
		String sql = "select reviewcount from T_Novel where novelid = ?";
		Object[] para = {novelid};
		ResultSet rs = bdao.select(sql,para);
		try {
			if(rs.next()){
				count = rs.getInt("reviewcount");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int reviewcount = count+1;
		String sql2 = "update T_Novel set reviewcount = ? where novelid= ?";
		Object[] para2 = {reviewcount,novelid};
		int row = bdao.update(sql2,para2);
		if (row>0) {
			return true;
		}
		else {
			return false;
		}
	}

	@Override
	public boolean increaseScanCounter(int novelid) {
		int count = 0 ;
		String sql = "select scancount from T_Novel where novelid = ?";
		Object[] para = {novelid};
		ResultSet rs = bdao.select(sql,para);
		try {
			if(rs.next()){
				count = rs.getInt("scancount");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int scancount = count+1;
		String sql2 = "update T_Novel set scancount = ? where novelid= ?";
		Object[] para2 = {scancount,novelid};
		int row = bdao.update(sql2,para2);
		if (row>0) {
			return true;
		}
		else {
			return false;
		}
	}

	@Override
	public V_Novel getVNovelByName(int novelid) {
		String sql = "select * from V_Novel where novelid = ?";
		Object[] para = {novelid};
		V_Novel vn = new V_Novel(bdao.select(sql,para));
		bdao.close();
		return vn;
	}

	@Override
	public T_Novel getTNovelByName(int novelid) {
		String sql = "select * from T_Novel where novelid=? ";
		Object[] para = {novelid};
		T_Novel tn = new T_Novel(bdao.select(sql,para));
		bdao.close();
		return tn;
	}

	@Override
	public List<V_Novel> getNovelListByPage(String userid, int currentPage,
			int pageSize) {
		List<V_Novel> list = null;
		String sql = "select * from V_Novel where userid=?";
		Object[] para = {userid};
		ResultSet rs = bdao.selectByPage(sql,para, currentPage, pageSize);
		if (rs != null) {
			list = V_Novel.toList(rs,pageSize);
		}
		bdao.close();
		return list;
	}

	@Override
	public int getNovelAmountByUser(String userid) {
		String sql = "select * from V_Novel where userid = ?";
		Object [] para= {userid};
		return bdao.selectCount(sql, para);
		
	}
		

	@Override
	public int getPageAmount(String userid, int pageSize) {
		String sql = "select * from V_Novel where userid=?";
		Object [] para = {userid};
		
		return bdao.pageAmount(sql, para, pageSize);
	}

	/*@Override
	public List<V_Novel> getNovelListByLike(String userid, String likecondtion,
			int currentPage, int pageSize) {
		List<V_Novel> list = null;
		String sql= "select * from V_Novel where userid = ? and novelname like ? "
				+ "order by createtime desc";
		Object[] para = {userid , "%"+likecondtion+"%"};
		ResultSet rs = bdao.selectByPage(sql, para, currentPage, pageSize);
		if (rs != null) {
			list = V_Novel.toList(rs,pageSize);
		}
		bdao.close();
		return list;
	}*/

	@Override
	public List<V_Novel> getBlogerUserNovelListByTopicLike(String likecondtion,
			int currentPage, int pageSize) {
		List<V_Novel> list = null;
		String sql= "select * from V_Novel where novelname like ? "
				+ "order by createtime desc";
		Object[] para = {"%"+likecondtion+"%"};
		ResultSet rs = bdao.selectByPage(sql, para, currentPage, pageSize);
		if (rs != null) {
			list = V_Novel.toList(rs,pageSize);
		}
		bdao.close();
		return list;
	}

	@Override
	public List<V_Novel> getForumsByUser(String userid) {
		List<V_Novel> list = null;
		ResultSet rs = null;
		if(userid!=null && userid !=""){
			String sql = "select * from V_Novel where userid=? order by createtime desc";
			Object[] param = {userid};
			rs = bdao.select(sql,param);
		}else{
			String sql = "select * from V_Novel order by createtime desc";
			rs = bdao.select(sql);
		}
		if(rs != null){
			list = V_Novel.toList(rs);
		}
		bdao.close();
		return list;
	}
	@Override
	public List<V_Novel> getMaxNovelScancount() {
		String sql = "select * from V_Novel order by scancount desc";
		ResultSet rs = bdao.select(sql);
		List<V_Novel> list = V_Novel.toList(rs);
		bdao.close();
		return list;
	}
	@Override
	public List<V_Novel> getAuthorNovel(String nickname) {
		String sql = "select * from V_Novel where nickname = ?";
		Object[] para = {nickname};
		ResultSet rs = bdao.select(sql,para);
		List<V_Novel> list = V_Novel.toList(rs);
		bdao.close();
		return list;
	}
	@Override
	public List<V_Novel> getListByNovelid(int novelid) {
		String sql = "select * from V_Novel where novelid = ?";
		Object[] para = {novelid};
		ResultSet rs = bdao.select(sql, para);
		List<V_Novel> list = V_Novel.toList(rs);
		bdao.close();
		return list;
	}

}
